package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.enums.TenantStatus;
import com.ruoyi.common.enums.UnitStatus;
import com.ruoyi.common.utils.DoubleUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收租费用Controller
 *
 * @author ruoyi
 * @date 2020-06-30
 */
@Controller
@RequestMapping("/system/expense")
public class SysExpenseController extends BaseController {
    private String prefix = "system/expense";

    @Autowired
    private ISysExpenseService sysExpenseService;

    @Autowired
    private ISysExpenseMergeService sysExpenseMergeService;

    @Autowired
    private ISysBuildingService sysBuildingService;

    @Autowired
    private ISysUnitService sysUnitService;

    @Autowired
    private ISysTenantService sysTenantService;

    @RequiresPermissions("system:expense:view")
    @GetMapping()
    public String expense() {
        return prefix + "/expense";
    }

    /**
     * 查询收租信息列表
     */
    @RequiresPermissions("system:expense:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysExpenseMerge sysExpenseMerge) {
        startPage();
        List<SysExpenseMerge> list = sysExpenseMergeService.selectSysExpenseMergeList(sysExpenseMerge);
        return getDataTable(list);
    }

    /**
     * 收租详情
     */
    @RequiresPermissions("system:expense:detail")
    @GetMapping("/detail/{expenseMergeId}")
    public String detail(@PathVariable("expenseMergeId") Long expenseMergeId, ModelMap mmap) {
        SysExpenseMerge sysExpenseMerge = sysExpenseMergeService.selectSysExpenseMergeById(expenseMergeId);
        sysExpenseMerge = sysExpenseMergeService.exchange(sysExpenseMerge);
        mmap.put("sysExpenseMerge", sysExpenseMerge);
        return prefix + "/detail";

    }


    /**
     * 导出收租费用列表
     */
//    @RequiresPermissions("system:expense:export")
//    @Log(title = "收租费用", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(SysExpense sysExpense)
//    {
//        List<SysExpense> list = sysExpenseService.selectSysExpenseList(sysExpense);
//        ExcelUtil<SysExpense> util = new ExcelUtil<SysExpense>(SysExpense.class);
//        return util.exportExcel(list, "expense");
//    }

    /**
     * 导出租金水电明细列表
     */
    @RequiresPermissions("system:expense:exportExpense")
    @Log(title = "收租费用", businessType = BusinessType.EXPORT)
    @PostMapping("/exportExpense")
    @ResponseBody
    public AjaxResult exportExpense(SysExpenseMerge sysExpenseMerge) {
        List<SysExpenseMerge> list = sysExpenseMergeService.selectSysExpenseMergeList(sysExpenseMerge);
        SysExpenseMerge sysExpenseAll = sysExpenseMergeService.selectSysExpenseMergeSum(sysExpenseMerge);
        List<SysExpenseExcel> excelList = new ArrayList<>();
        //数据转换
        SysExpenseExcel allSysExpenseExcel = new SysExpenseExcel();
        BeanUtils.copyProperties(sysExpenseAll, allSysExpenseExcel);

        for (int i = 0; i < list.size(); i++) {
            SysExpenseExcel sysExpenseExcel = new SysExpenseExcel();
            BeanUtils.copyProperties(list.get(i), sysExpenseExcel);
            sysExpenseExcel.setNumber(String.valueOf(i + 1));
            excelList.add(sysExpenseExcel);
        }
        allSysExpenseExcel.setTenantName("");
        allSysExpenseExcel.setUnitNameStr("");
        allSysExpenseExcel.setNumber("合计");
        excelList.add(allSysExpenseExcel);

        ExcelUtil<SysExpenseExcel> util = new ExcelUtil<SysExpenseExcel>(SysExpenseExcel.class);
        return util.exportExcel(excelList, "租金水电明细");
    }

    /**
     * 新增收租费用
     */
//    @GetMapping("/add")
//    public String add(ModelMap mmap)
//    {
//        mmap.put("building", sysBuildingService.selectSysBuildingAll());
//        return prefix + "/add";
//    }

    /**
     * 新增收租-单元下拉列表
     */
//    @PostMapping("/addUnitList")
//    @ResponseBody
//    public AjaxResult addUnitList(Long buildingId)
//    {
//        List<SysUnit> unitList = sysUnitService.selectSysUnitByBuildingId(buildingId);
//        if(unitList!=null && !unitList.isEmpty()){
//            return AjaxResult.success(unitList);
//        }
//        return AjaxResult.error();
//    }

    /**
     * 新增收租-租户下拉列表
     */
//    @PostMapping("/addTenantList")
//    @ResponseBody
//    public AjaxResult addTenantList(String unitIdStr)
//    {
//        List<SysTenant> tenantList = sysTenantService.selectSysTenantByUnitId(unitIdStr);
//        if(tenantList!=null && !tenantList.isEmpty()){
//            tenantList.removeIf(e->e.getTenantStatus().equals(TenantStatus.LEAVE.getCode())
//                    || e.getTenantStatus().equals(TenantStatus.WILL.getCode()));
//            return AjaxResult.success(tenantList);
//        }
//        return AjaxResult.error();
//    }

    /**
     * 新增收租-信息填写
     */
//    @PostMapping("/addTenantAndExpense")
//    @ResponseBody
//    public AjaxResult addTenantAndExpense(Long tenantId)
//    {
//        Map resultMap = new HashMap();
//        //返回租户基础信息
//        SysTenant sysTenant = sysTenantService.selectSysTenantById(tenantId);
//        //返回租户收费信息
//        SysExpense sysExpense = sysExpenseService.selectSysExpenseByTenantId(tenantId);
//        //返回公摊比例
//        float apportionRatio = 0;
//        float allArea = 0;
//        if(sysTenant != null){
//            List<SysTenant> list = sysTenantService.selectSysTenantByBuildingId(sysTenant.getBuildingId());
//            for (SysTenant tenant : list) {
//                allArea += Float.parseFloat(tenant.getTenantArea());
//            }
//            apportionRatio = Float.parseFloat(sysTenant.getTenantArea())/allArea;
//        }
//        if(sysTenant!=null){
//            resultMap.put("sysTenant",sysTenant);
//        }
//        if(sysExpense!=null){
//            resultMap.put("sysExpense",sysExpense);
//        }
//        resultMap.put("apportionRatio",apportionRatio);
//        return AjaxResult.success(resultMap);
//    }

    /**
     * 新增保存收租费用
     */
//    @RequiresPermissions("system:expense:add")
//    @Log(title = "收租费用", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(SysExpense sysExpense)
//    {
//        sysExpense.setCreateBy(ShiroUtils.getLoginName());
//        ShiroUtils.clearCachedAuthorizationInfo();
//        return toAjax(sysExpenseService.insertSysExpense(sysExpense));
//    }

    /**
     * 修改收租费用
     */
//    @GetMapping("/edit/{expenseId}")
//    public String edit(@PathVariable("expenseId") Long expenseId, ModelMap mmap)
//    {
//        SysExpense sysExpense = sysExpenseService.selectSysExpenseById(expenseId);
//        double result = new BigDecimal(Double.parseDouble(sysExpense.getApportionRatio())*100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//        String apportionRatio = result+"%";
//        mmap.put("building", sysBuildingService.selectSysBuildingAll());
//        mmap.put("apportionRatio",apportionRatio);
//        mmap.put("sysExpense", sysExpense);
//        return prefix + "/edit";
//    }

    /**
     * 修改保存收租费用
     */
//    @RequiresPermissions("system:expense:edit")
//    @Log(title = "收租费用", businessType = BusinessType.UPDATE)
//    @PostMapping("/edit")
//    @ResponseBody
//    public AjaxResult editSave(SysExpense sysExpense)
//    {
//        sysExpense.setUpdateBy(ShiroUtils.getLoginName());
//        ShiroUtils.clearCachedAuthorizationInfo();
//        return toAjax(sysExpenseService.updateSysExpense(sysExpense));
//    }

    /**
     * 删除收租费用
     */
    @RequiresPermissions("system:expense:remove")
    @Log(title = "收租费用", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysExpenseMergeService.deleteSysExpenseMergeByIds(ids));
    }

}
